let FilterBuilder = function() {
  this.filters = [];
};

FilterBuilder.prototype.removeFitler = function(field, op) {
  if (op === undefined) {
    op = "eq";
  }
  this.filters.every((el, idx) => {
    if (el.op == op && el.field == field) {
      this.filters.splice(idx, 1);
      return false;
    }
    return true;
  });
  return this;
};

FilterBuilder.prototype.getFilters = function() {
  return this.filters;
};

FilterBuilder.prototype.toString = function() {
  return JSON.stringify(this.getFilters());
};

FilterBuilder.prototype.resetFilters = function() {
  this.filters = [];
};

FilterBuilder.prototype.addFilter = function(field, op, value) {
  if (typeof field === "object") {
    op = field.op;
    value = field.value;
    field = field.field;
  }
  if (value === undefined) {
    value = op;
    op = "eq";
  }
  if (!op) {
    op = "eq";
  }
  let hasOp = false;
  this.filters.every(el => {
    if (el.op == op && el.field == field) {
      el.value = value;
      hasOp = true;
      return false;
    }
    return true;
  });
  if (!hasOp) {
    //如果有op
    this.filters.push({
      field,
      op,
      value
    });
  }
  return this;
};

export default FilterBuilder;
